package gov.fnal.mcginnis.threetone;

import java.net.URL;
import java.util.Date;

import com.astrofizzbizz.simpleXml.SimpleXmlException;
import com.astrofizzbizz.simpleXml.SimpleXmlReader;
import com.astrofizzbizz.simpleXml.SimpleXmlWriter;


public class ThreeTonePalette 
{
	public ThreeTonePaletteColor[] paletteColor;
	public class ThreeTonePaletteColor
	{
		public String rgbHex;
		public String title;
		public ThreeTonePaletteColor(SimpleXmlReader colorTag) throws SimpleXmlException
		{
			title = colorTag.attribute("title");
			rgbHex = colorTag.attribute("rgbHex");
		}
		public ThreeTonePaletteColor(String title, String rgbHex)
		{
			this.title = title;
			this.rgbHex = rgbHex;
		}
		public ThreeTonePaletteColor() 
		{
			this.title = "";
			this.rgbHex = "C8C8C8";
		}
	}
	public ThreeTonePalette(int inumColors)
	{
		paletteColor = new ThreeTonePaletteColor[inumColors];
		for (int icol = 0; icol < inumColors; ++icol)
		{
			paletteColor[icol] = new ThreeTonePaletteColor();
		}
	}
	public int numColors()
	{
		return paletteColor.length;
	}
	public ThreeTonePalette(String FileName) throws SimpleXmlException
	{
		SimpleXmlReader paletteFileTag = new SimpleXmlReader(FileName);
		SimpleXmlReader colorTags = paletteFileTag.tagsByName("color");
		int inumColors = colorTags.numChildTags();
		paletteColor = new ThreeTonePaletteColor[inumColors];
		for (int icol = 0; icol < inumColors; ++icol)
		{
			SimpleXmlReader colorTag = colorTags.tag(icol);
			paletteColor[icol] = new ThreeTonePaletteColor(colorTag);
		}
	}
	public ThreeTonePalette(URL resourceURL) throws SimpleXmlException 
	{
		SimpleXmlReader paletteFileTag = new SimpleXmlReader(resourceURL.getPath());
		SimpleXmlReader colorTags = paletteFileTag.tagsByName("color");
		int inumColors = colorTags.numChildTags();
		paletteColor = new ThreeTonePaletteColor[inumColors];
		for (int icol = 0; icol < inumColors; ++icol)
		{
			SimpleXmlReader colorTag = colorTags.tag(icol);
			paletteColor[icol] = new ThreeTonePaletteColor(colorTag);
		}
	}
	public void writePalette(String fileName) throws SimpleXmlException
	{
		SimpleXmlWriter xw = new SimpleXmlWriter("Created " + new Date());
		xw.openXmlTag("palette");
		for (int icol = 0; icol < numColors(); ++icol)
		{
			xw.openXmlTag("color");
			xw.setAttribute("rgbHex", paletteColor[icol].rgbHex);
			xw.setAttribute("title", paletteColor[icol].title);
			xw.setAttribute("index", Integer.toString(icol));
			xw.closeXmlTag("color");
		}		
		xw.closeXmlTag("palette");
		xw.saveXmlDocument(fileName);
	}
	public static void main(String[] args) 
	{

	}
}
